/************************友情链接js*********************************/
define(function(require) {
    var $ = require("jquery1.7");
	var utils = require("utils");
	var ctx = utils.constants.ctx;
	var Validator = require("validator");
	var ui = require("ui");
	var friendLinkService=require("bean/friendLinkService");
	
	//初始表格
	var layer = layui.layer;
	var form = layui.form;
	var element = layui.element;
	var table = layui.table;
	
	var tablelin = table.render({
	    elem: '#demo'
	    ,url : '/back/website/friendLinkData'
	    ,cols: [[ //标题栏
	      {checkbox: true}
	      ,{field: 'title', title: '网站名', width: 500}
	      ,{field: 'linkUrl', title: '网址', width:600}
	      ,{fixed: 'right', float:'right',width: 500, align:'center', templet:'#table_ope',title:'操作'}
	      ]] 
	    ,even: true
	    ,page: true //是否显示分页
	    ,limits: [8, 12, 15,18]
	    ,limit: 8 //每页默认显示的数量
	    ,id: "test"
	  });
	
	//重载表格事件
	$(document).on("click","#searchBtn",function(){		
		var param = {
				title:$("#search_input").val()
		}
		tablelin.reload({
			where:param
		});
	})
	
	//添加网站
	$(document).on("click","#addFriendLinkBtn",function(){
		$.post(ctx + '/back/website/friendLinkUpdate',function(data){

			layer.open({
				title: '添加网站',
				type:1,
				content:data
			})
			
		});
		$("#friendUrl").blur(function(){
			var val = $("#friendUrl").val();
			if(val.indexOf("http://")==-1 && val.indexOf("https://")==-1){
				blackBox.alert("请输入以http或https开头的网址！");
			}
		});
	})
	
	//工具条事件
	table.on('tool(demo)',function(obj){
		  var layEvent = obj.event; //获得 lay-event 对应的值
		  var data = obj.data;
		  var friendLinkId = data.id;
		  var friendOrderId = data.orderId;
		  
		  if(layEvent==='edit'){
			  $.post(ctx+'/back/website/friendLinkUpdate',{"friendId":friendLinkId},function(str){
				 layer.open({
				    title: '修改网站',
				    type:1,
				    content:str
			      }) 
			  })
		  }else if(layEvent==='moveUp'){
			  friendLinkService.friendLinkMoveUpAndDown(friendLinkId,friendOrderId,"up");
			  window.location.reload();
		  }else{
			  friendLinkService.friendLinkMoveUpAndDown(friendLinkId,friendOrderId,"down");
    		  window.location.reload();
		  }
	})
	
	
	form.on('submit(firendLink_sumit)',function(data){
		 if(data.field.id=="" || data.field.id==null){
				friendLinkService.addFriendLink(data.field);
 	 		    alert('添加成功');
			 }else{
				 friendLinkService.updateFriendLink(data.field);
				 alert('修改成功');
			 }
	       location.reload()
	})

	
	//删除
		$("#deleteaddFriendLinkBtn").bind("click", function(){
        var checkStatus = layui.table.checkStatus('test');
		if (checkStatus.data.length===0) {
			layer.alert("请选择网站");
			return;
		}
		layer.confirm("确定要删除网站？",{icon: 3, title:'提示'},function(isOk){
			if(isOk){
				var data = checkStatus.data;
				var array = [];
				for(var i = 0; i<data.length;i++){
					array.push(data[i].id);   
				}
				if (array.length != 0) {
					try{
						ui.mask().show("请稍候...");
						friendLinkService.delFriendLink(array);
						ui.mask().hide();
						layer.alert("删除成功",function(){							
							window.location.reload(true);
						});
					} catch (e) {
						ui
						.mask().hide();
						layer.alert("删除失败:"+e.message);
					}
				}
			}
		})
	});
	
	
	
//	//上移
//	$(document).on("click",".moveTopFriendLink",function(){
//		var friendId=$(this).attr("dataId").replace("friendLink_","");
//		var friendOrderId=$(this).attr("data").replace("friendLink_","");
//		friendLinkService.friendLinkMoveUpAndDown(friendId,friendOrderId,"up");
//		window.location.reload();
//	});
//	
//	//下移
//	$(document).on("click",".moveDownFriendLink",function(){
//		var friendId=$(this).attr("dataId").replace("friendLink_","");
//		var friendOrderId=$(this).attr("data").replace("friendLink_","");
//		friendLinkService.friendLinkMoveUpAndDown(friendId,friendOrderId,"down");
//		window.location.reload();
//	});
		
});

